package com.chuangke.mds.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.exception.ChuangkeException;
import com.chuangke.common.idgen.IdManager;
import com.chuangke.mds.dao.KnowledgeModelDao;
import com.chuangke.mds.dao.KnowledgeModelPropertyDao;
import com.chuangke.mds.entity.KnowledgeModel;
import com.chuangke.mds.entity.KnowledgeModelProperty;
import com.chuangke.mds.service.KnowledgeModelService;

import cn.hutool.core.util.StrUtil;

@Service
@Transactional(rollbackFor = Throwable.class)
public class KnowledgeModelServiceImpl implements KnowledgeModelService {

    @Autowired
    private KnowledgeModelDao knowledgeModelDao;
    @Autowired
    private KnowledgeModelPropertyDao knowledgeModelPropertyDao;

    @Override
    public void save(KnowledgeModel knowledgeModel) {
        if (StrUtil.isEmpty(knowledgeModel.getName())) {
            throw new ChuangkeException("请输入标题");
        }

        if (StrUtil.isBlank(knowledgeModel.getId())) {
            knowledgeModel.setId(IdManager.nextId());
        }

        if (CollectionUtils.isNotEmpty(knowledgeModel.getPropertyList())) {
            knowledgeModel.getPropertyList().forEach(p -> {
                p.setModelId(knowledgeModel.getId());
            });
        }

        if (CollectionUtils.isNotEmpty(knowledgeModel.getPropertyList())) {
            knowledgeModelPropertyDao.saveOrUpdateBatch(knowledgeModel.getPropertyList()) ;
        }
        knowledgeModelDao.saveOrUpdate(knowledgeModel) ;
    }

    @Override
    public void delete(List<String> idList) {
        knowledgeModelDao.removeByIds(idList) ;
        knowledgeModelPropertyDao.remove(new QueryWrapper<KnowledgeModelProperty>().lambda().in(KnowledgeModelProperty::getModelId, idList));
    }

    @Override
    public KnowledgeModel load(String id) {
        KnowledgeModel knowledge = knowledgeModelDao.getById(id) ;
        List<KnowledgeModelProperty> affixList = knowledgeModelPropertyDao.list(new QueryWrapper<KnowledgeModelProperty>().lambda().eq(KnowledgeModelProperty::getModelId, id)) ;
        knowledge.setPropertyList(affixList);
        return knowledge;
    }

    @Override
    public List<KnowledgeModelProperty> findModelPropertyList(String modelId) {
        List<KnowledgeModelProperty> affixList = knowledgeModelPropertyDao.list(new QueryWrapper<KnowledgeModelProperty>().lambda().eq(KnowledgeModelProperty::getModelId, modelId)) ;
        return affixList;
    }

    @Override
    public IPage<KnowledgeModel> page(CkPageQuery page) {
        QueryWrapper<KnowledgeModel> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(page.getAsString("name"))) {
            qw.lambda().like(KnowledgeModel::getName, page.getAsString("name")) ;
        }

        qw.lambda().orderByDesc(KnowledgeModel::getCreatedAt) ;
        return knowledgeModelDao.page(page.getPage(),qw);
    }
}
